package com.huatai.exchange.integration.base.impl;

import com.faneui.common.exception.BusinessException;
import com.faneui.integration.http.HttpClientUtils;
import com.google.common.collect.Maps;
import com.huatai.base.facade.bean.communicate.CommunicationMailVo;
import com.huatai.base.facade.bean.communicate.MailCommunicationReq;
import com.huatai.base.facade.bean.communicate.QueryCommunicationMailVoListResp;
import com.huatai.base.facade.bean.communicate.SztCommunicationReq;
import com.huatai.exchange.integration.base.AbstractBaseService;
import com.huatai.exchange.integration.base.service.CommunicationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CommunicationServiceImpl extends AbstractBaseService implements CommunicationService {

    @Override
    public void sendSzt(String toSmbPathUrl, String fileNames, String filePaths) throws BusinessException {
        String url = baseUrl + "/communication/szt/send";
        SztCommunicationReq req = new SztCommunicationReq();
        req.setSmbFilePath(toSmbPathUrl);
        req.setFileOrUrls(filePaths);
        req.setFileNames(fileNames);
        HttpClientUtils.post(url, req);
    }

    @Override
    public void sendMail(String title, String content, String toMails) throws BusinessException {
        sendMail(null, null, null, title, content, toMails  );
    }

    @Override
    public void sendMail(String title, String content, String toMails, String fileNames) throws BusinessException {
        sendMail(null,null,null,title,content,toMails,null,fileNames);
    }

    @Override
    public void sendMail(String title, String content, String toMails, String fileNames, String filePaths) throws BusinessException {
        sendMail(null,null,null,title,content,toMails,fileNames,filePaths);
    }

    @Override
    public void sendMail(String fromMail, String password, String fromServerHost, String title, String content, String toMail) throws BusinessException {
        sendMail(fromMail, password, fromServerHost, title, content, toMail, null,null);
    }

    @Override
    public void sendMail(String fromMail, String password, String fromServerHost, String title, String content, String toMail, String fileNames,String filePaths) throws BusinessException {
        String url = baseUrl + "/communication/mail/send";
        MailCommunicationReq req = new MailCommunicationReq();
        req.setTitle(title);
        req.setContent(content);
        if(StringUtils.isNotBlank(fromMail)) req.setFromMail(fromMail);
        if(StringUtils.isNotBlank(password)) req.setPassword(password);
        if(StringUtils.isNotBlank(fromServerHost)) req.setFromServerHost(fromServerHost);
        req.setToMails(toMail);
        req.setCommunicationType("mail");
        if(StringUtils.isNotBlank(fileNames))
            req.setFileNames(fileNames);
        if(StringUtils.isNotBlank(filePaths))
            req.setFileOrUrls(filePaths);
        HttpClientUtils.post(url, req);
    }

    @Override
    public List<CommunicationMailVo> queryMail(String occurDate,String fromMail, String toMails, String status) throws BusinessException {
        return queryMail(occurDate,fromMail, toMails, status,null,null,null);
    }

    @Override
    public List<CommunicationMailVo> queryMail(String occurDate,String fromMail, String toMails, String status, String title, String content, String fileOrUrls) throws BusinessException {
        String url = baseUrl + "/communication/mail/list";
        Map<String, Object> map = Maps.newHashMap();
        if (StringUtils.isNotBlank(occurDate)) map.put("occurDate", occurDate);
        if (StringUtils.isNotBlank(fromMail)) map.put("fromMail", fromMail);
        if (StringUtils.isNotBlank(toMails)) map.put("toMails", toMails);
        if (StringUtils.isNotBlank(status)) map.put("status", status);
        if (StringUtils.isNotBlank(title)) map.put("title", title);
        if (StringUtils.isNotBlank(content)) map.put("content", content);
        if (StringUtils.isNotBlank(fileOrUrls)) map.put("fileOrUrls", fileOrUrls);
        return HttpClientUtils.get(url, map, QueryCommunicationMailVoListResp.class).getCommnunicationMailVos();
    }
}
